package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.Result;
import com.itheima.pojo.Question;
import com.itheima.service.HealthQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.ArrayList;
import java.util.Collections;


@RestController
@RequestMapping("/healthQuestion")
public class HealthQuestionController {

    @Autowired
    private JedisPool jedisPool;

    @Reference(version = "1.0.0")
    private HealthQuestionService healthQuestionService;

    @PostMapping("/find")
    public Result find(Integer id, Integer value) {
        if (id == 66) {
            Jedis jedis = null;
            try {
                jedis = jedisPool.getResource();
                ArrayList<String> strings = new ArrayList<>();
                for (int i = 1; i < 67; i++) {
                    String s = jedis.get(i + "");
                    strings.add(s);
                }
                int one = 0;
                for (int i = 0; i < 7; i++) {
                    one += Integer.parseInt(strings.get(i));
                }

                int two = 0;
                for (int i = 7; i < 14; i++) {
                    two += Integer.parseInt(strings.get(i));
                }

                int three = 0;
                for (int i = 15; i < 22; i++) {
                    three += Integer.parseInt(strings.get(i));
                }

                int four = 0;
                for (int i = 23; i < 30; i++) {
                    four += Integer.parseInt(strings.get(i));
                }

                int five = 0;
                for (int i = 31; i < 37; i++) {
                    five += Integer.parseInt(strings.get(i));
                }

                int six = 0;
                for (int i = 38; i < 44; i++) {
                    six += Integer.parseInt(strings.get(i));
                }

                int seven = 0;
                for (int i = 45; i < 51; i++) {
                    seven += Integer.parseInt(strings.get(i));
                }

                int eight = 0;
                for (int i = 52; i < 58; i++) {
                    eight += Integer.parseInt(strings.get(i));
                }

                int nine = 0;
                for (int i = 59; i < 65; i++) {
                    nine += Integer.parseInt(strings.get(i));
                }

                one -= 7;
                int one1 = one / (7 * 4) * 100;
                two -= 8;
                int two1 = two / (8 * 4) * 100;
                three -= 8;
                int three1 = three / (8 * 4) * 100;
                four -= 8;
                int four1 = four / (8 * 4) * 100;

                five -= 7;
                int five1 = five / (7 * 4) * 100;
                six -= 7;
                int six1 = six / (7 * 4) * 100;
                seven -= 7;
                int seven1 = seven / (7 * 4) * 100;
                eight -= 7;
                int eight1 = eight / (7 * 4) * 100;
                nine -= 7;
                int nine1 = nine / (7 * 4) * 100;
                if (nine1 >= 60 & one1 < 30 & two1 < 30 & three1 < 30 & four1 < 30 & five1 < 30 & six1 < 30 & seven1 < 30 & eight1 < 30) {
                    Question question = new Question();
                    question.setId(67);
                    question.setQuestion("您的体质为平和质");
                    return Result.success(question);
                }
                if (nine1 >= 40 & one1 < 40 & two1 < 40 & three1 < 40 & four1 < 40 & five1 < 40 & six1 < 40 & seven1 < 40 & eight1 < 40) {
                    Question question = new Question();
                    question.setId(67);
                    question.setQuestion("您的体质为平和质");
                    return Result.success(question);
                }
                ArrayList<Integer> integers = new ArrayList<>();
                integers.add(one1);
                integers.add(two1);
                integers.add(three);
                integers.add(four1);
                integers.add(five1);
                integers.add(six1);
                integers.add(seven1);
                integers.add(eight1);
                Integer max = Collections.max(integers);
                if(max==one1){
                    Question question = new Question();
                    question.setId(67);
                    question.setQuestion("您的体质为阳虚体质");
                    return Result.success(question);
                }
                if(max==two1){
                    Question question = new Question();
                    question.setId(67);
                    question.setQuestion("您的体质为阳虚体质");
                    return Result.success(question);
                }

                if(max==three1){
                    Question question = new Question();
                    question.setId(67);
                    question.setQuestion("您的体质为气虚体质");
                    return Result.success(question);
                }

                if(max==four1){
                    Question question = new Question();
                    question.setId(67);
                    question.setQuestion("您的体质为痰湿体质");
                    return Result.success(question);
                }

                if(max==five1){
                    Question question = new Question();
                    question.setId(67);
                    question.setQuestion("您的体质为湿热体质");
                    return Result.success(question);
                }

                if(max==six1){
                    Question question = new Question();
                    question.setId(67);
                    question.setQuestion("您的体质为血瘀体质");
                    return Result.success(question);
                }
                if(max==seven1){
                    Question question = new Question();
                    question.setId(67);
                    question.setQuestion("您的体质为血瘀体质");
                    return Result.success(question);
                }

                if(max==eight1){
                    Question question = new Question();
                    question.setId(67);
                    question.setQuestion("您的体质为气郁型");
                    return Result.success(question);
                }

                if(max==nine1){
                    Question question = new Question();
                    question.setId(67);
                    question.setQuestion("您的体质为平和质");
                    return Result.success(question);
                }

               /* Question question = new Question();
                question.setId(67);
                question.setQuestion("您的体质为阴虚体质");
                return Result.success(question);*/
            } catch (Exception e) {
                e.printStackTrace();
                return Result.fail(-999, "后台异常，请重新答题");
            } finally {
                if (jedis != null) {
                    jedis.close();
                }
            }

        }


        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            if (id >= 61) {
                jedis.set(id + "", (6 - value) + "");
            } else {
                jedis.set(id + "", value + "");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(-999, "后台异常，请重新答题");
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
        id += 1;
        return healthQuestionService.find(id);
    }


}
